package BS.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;


import BS.entity.guanliyuan;
import BS.utils.propertiesUtils;

public class guanliyuanDao {
	public void add(guanliyuan guanliyuan) throws SQLException {
		QueryRunner queryRunner = new QueryRunner(propertiesUtils.getDataSource());
		String sql = "insert into guanliyuan(name,num,psd) values(?,?,?)";
		queryRunner.update(sql,guanliyuan.getName(),guanliyuan.getNum(),guanliyuan.getPsd());
	}
	public void delete(Integer id) throws SQLException {
		QueryRunner queryRunner = new QueryRunner(propertiesUtils.getDataSource());
		String sql = "delete from guanliyuan where id = ?";
		queryRunner.update(sql,id);
	}
	public void update(guanliyuan guanliyuan) throws SQLException {
		QueryRunner queryRunner = new QueryRunner(propertiesUtils.getDataSource());
		String sql = "update guanliyuan set gid = ?,name = ?,num = ? where id = ?";
		queryRunner.update(sql,guanliyuan.getName(),guanliyuan.getNum(),guanliyuan.getPsd(),guanliyuan.getId());
	}
	public List<guanliyuan> list(guanliyuan guanliyuan) throws SQLException {
		QueryRunner queryRunner = new QueryRunner(propertiesUtils.getDataSource());
		String sql = "select * from guanliyuan";
		List<guanliyuan> list = queryRunner.query(sql,new BeanListHandler<>(guanliyuan.class));
		return list;
	}
	public guanliyuan findByid(Integer id) throws SQLException {
		QueryRunner queryRunner = new QueryRunner(propertiesUtils.getDataSource());
		String sql = "select * from guanliyuan where id = ?";
		guanliyuan guanliyuan = queryRunner.query(sql,new BeanHandler<>(guanliyuan.class),id);
		return guanliyuan;
	}
	public guanliyuan login(String gid,String psd) throws SQLException {
		QueryRunner queryRunner = new QueryRunner(propertiesUtils.getDataSource());
		String sql = "select * from guanliyuan where gid = ? and psd = ?";
		guanliyuan entity = queryRunner.query(sql,new BeanHandler<>(guanliyuan.class),gid,psd);
		return entity;
	}
	public void update(String pwd, Integer id)throws SQLException {
		QueryRunner queryRunner = new QueryRunner(propertiesUtils.getDataSource());
		String sql = "update guanliyuan set psd = ? where id = ? ";
		queryRunner.update(sql, pwd,id);
	}
}
